/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view.book;

import controllers.*;
import constants.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;
public class CheckOutPanel extends javax.swing.JPanel {
    public CheckOutPanel(AbstractTableModel tableModel) {
        initComponents();
        MainController = new MainController();
        tbCheckout.setModel(tableModel);  
        tbCheckout.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        tbCheckout.getSelectionModel().addListSelectionListener(new ListSelection());
        
    }
    
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tbCheckout = new javax.swing.JTable();
        btView = new javax.swing.JButton();
        btPrint = new javax.swing.JButton();
        btDel = new javax.swing.JButton();
        btEdit = new javax.swing.JButton();
        btFind1 = new javax.swing.JButton();
        btSuccess = new javax.swing.JButton();

        setPreferredSize(new java.awt.Dimension(874, 355));

        tbCheckout.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(tbCheckout);

        btView.setFont(new java.awt.Font("Tahoma", 0, 13)); // NOI18N
        btView.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/details.png"))); // NOI18N
        btView.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btViewActionPerformed(evt);
            }
        });

        btPrint.setFont(new java.awt.Font("Tahoma", 0, 13)); // NOI18N
        btPrint.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/bil.png"))); // NOI18N
        btPrint.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btPrintActionPerformed(evt);
            }
        });

        btDel.setFont(new java.awt.Font("Tahoma", 0, 13)); // NOI18N
        btDel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/del.png"))); // NOI18N
        btDel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btDelActionPerformed(evt);
            }
        });

        btEdit.setFont(new java.awt.Font("Tahoma", 0, 13)); // NOI18N
        btEdit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/edit1.png"))); // NOI18N
        btEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btEditActionPerformed(evt);
            }
        });

        btFind1.setFont(new java.awt.Font("Tahoma", 0, 13)); // NOI18N
        btFind1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/booking.png"))); // NOI18N
        btFind1.setText(" đặt phòng lại");

        btSuccess.setFont(new java.awt.Font("Tahoma", 0, 13)); // NOI18N
        btSuccess.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/checkin.png"))); // NOI18N
        btSuccess.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSuccessActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btEdit, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btDel, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btView, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btPrint, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btSuccess, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btFind1)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 854, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btSuccess, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(btFind1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(btEdit, javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(btDel, javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(btView, javax.swing.GroupLayout.Alignment.TRAILING))
                    .addComponent(btPrint))
                .addGap(20, 20, 20)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 10, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btViewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btViewActionPerformed
            if(tbCheckout.getSelectedRowCount()>0){
            getId();       
            new CheckOutDlg(new JFrame(),tbCheckout, true,true, id).setVisible(true);
            }else  
            JOptionPane.showMessageDialog(this, "Vui lòng chọn khách hàng muốn xem"); 
    }//GEN-LAST:event_btViewActionPerformed

    private void btDelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btDelActionPerformed
                
       if(tbCheckout.getSelectedRowCount()>0){
            int choice = JOptionPane.showConfirmDialog(this," Chắc chắn muốn xóa ?", "Xóa",JOptionPane.YES_NO_OPTION);
            if(choice== JOptionPane.YES_OPTION){
                getId();
                MainController.removeAll(id);
                ((model.InfoBookingModel) tbCheckout.getModel()).removeRow(realIndex);
                System.out.println("Xóa hàng trên giao diện");
                tbCheckout.repaint();                        
           }
           
       }else{
           JOptionPane.showMessageDialog(this,"Vui lòng chọn khách hàng cần xóa");
       }
    }//GEN-LAST:event_btDelActionPerformed

    private void btEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btEditActionPerformed
            
    }//GEN-LAST:event_btEditActionPerformed

    private void btPrintActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btPrintActionPerformed
        if(tbCheckout.getSelectedRowCount()>0){
                getId();               
                int choice = JOptionPane.showConfirmDialog(this,"Tạo hóa đơn ?", "Hoàn tất",JOptionPane.YES_NO_OPTION);
                if(choice== JOptionPane.YES_OPTION){                
                 int bill=PaymentController.insert(getPayment());
                 new ExportBill(id,bill).printBill("bill.html"); 
                }
        }
        
    }//GEN-LAST:event_btPrintActionPerformed

    private void btSuccessActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSuccessActionPerformed
           if(tbCheckout.getSelectedRowCount()>0){
                getId();
                int choice = JOptionPane.showConfirmDialog(this," Xác nhận khách hàng đã trả phòng và thanh toán đầy đủ ?", "Hoàn tất",JOptionPane.YES_NO_OPTION);
                if(choice== JOptionPane.YES_OPTION){                
                InfoBookController.updateStatus(id,Constants.STATUS_FULL);
                JOptionPane.showMessageDialog(this,"Thay đổi thành công "); 
                ((model.InfoBookingModel) tbCheckout.getModel()).removeRow(realIndex);
                System.out.println("Xóa hàng trên giao diện");
                tbCheckout.repaint();
           }
        }
    }//GEN-LAST:event_btSuccessActionPerformed
    private entity.Payment getPayment(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        entity.Payment pay = new entity.Payment();
        entity.Customer customer = new entity.Customer();
        customer = BookingController.getAllByCustomer(id).get(0).getCustomer();
        pay.setCost_room(customer.getInfo().getTotal());
        pay.setCost_service(0);
        pay.setTotal(pay.getCost_room());
        pay.setDate(sdf.format(new Date()));
        pay.setId_customer(id);
        pay.setStatus(Constants.STATUS_FULL);       
        return pay;
    }
    private void getId(){
        realIndex = tbCheckout.convertRowIndexToModel(tbCheckout.getSelectedRow());
        id = (int) ((model.InfoBookingModel) tbCheckout.getModel()).getValueAt(realIndex,1);
    }
    private int id;
    private int realIndex;
    private MainController MainController;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btDel;
    private javax.swing.JButton btEdit;
    private javax.swing.JButton btFind1;
    private javax.swing.JButton btPrint;
    private javax.swing.JButton btSuccess;
    private javax.swing.JButton btView;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tbCheckout;
    // End of variables declaration//GEN-END:variables
    class ListSelection implements ListSelectionListener {

  
        public void valueChanged(ListSelectionEvent e) {
            

                        if(!e.getValueIsAdjusting()){
                         
                        }          
            }        
        }
}
